<?php
function url_unpack_slow($url) {
    print "unpacking $url\n";
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return $info['url'];
};

function url_unpack($url, $iter_num = 0) {
    if ($iter_num > 5) {
        print "Too many iterations!\n";
        return $url;
    };
    if (strlen($url)>25) {
        print "Too long url to unpack!\n";
        return $url;
    };

    print "unpacking $url\n";
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

    $http_code = $info['http_code'];
    if ($http_code == 301 || $http_code == 302 || $http_code == 0) {
        // print "Moved!\n";
        if (strlen($response) < 2048) {
            print ("Inspecting headers\n");
            $lines = explode("\n", $response);
            $location = false;
            foreach ($lines as $num => $line) {
                if (strpos($line, "Location:") !== false) {
                    print ("Found location ".$line."\n");
                    $parts = explode(" ", $line);
                    $location = $parts[1];
                };
            };
            if ($location) {
                if (strlen($location) < 25) {
                    print("need to unpack one more times\n");
                        $location = url_unpack($location, $iter_num + 1);
                    };
                return $location;
            } else {
                echo "Cant find location! Response: \n";
                echo $response;
            }

        } else {
            print "Response is too long. No need to unpack?\n";
            return $url;
        };
    } else {
        print "Done. Http_code: $http_code \n";
    };
    print "Hmmmm....";
    return $info["url"];
    
};

function parse_urls($text) {
    $urls = array();
    while (preg_match('/http:\/\/[a-zA-Z0-9\-_%\.\/]+/', $text, $matches) ) {
        $url = $matches[0];
        $text = str_replace($url, "", $text);
        array_push($urls, $url);
    };
    return $urls;
};

function unpack_urls($text) {
    $urls = parse_urls($text);
    if (count($urls) == 0) return $text;
    print("found ".count($urls)." urls!\n");
    foreach ($urls as $num => $url) {
        $long_url = url_unpack_slow($url);
        if ($long_url != $url) {
            print "replacing $url to $long_url\n";
            $text = str_replace($url, $long_url, $text);
        };
    };
    return $text;
};

?>
